package com.xiaozhi.libraryfunction.service.impI;


import com.xiaozhi.common.constant.UserConstants;
import com.xiaozhi.common.core.text.Convert;
import com.xiaozhi.common.utils.StringUtils;
import com.xiaozhi.libraryfunction.domain.LibraryBorrow;
import com.xiaozhi.libraryfunction.mapper.LibraryBorrowMapper;
import com.xiaozhi.libraryfunction.service.LibraryBorrowService;
import com.xiaozhi.libraryfunction.service.LibraryBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 图书借阅信息 服务层
 * 
 * @author ruoyi
 */
@Service
public class LibraryBorrowServiceImpl implements LibraryBorrowService
{
    @Autowired
    private LibraryBorrowMapper libraryBorrowMapper;

    /**
     * 获取图书借阅列表
     *
     * @param borrow 借阅信息
     * @return
     */
    @Override
    public List<LibraryBorrow> selectLibraryBorrowList(LibraryBorrow borrow,String user_name,String name,Integer typeId)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_name",user_name);
        map.put("name",name);
        map.put("typeId",typeId);
        map.put("backType",borrow.getBackType());
        return libraryBorrowMapper.selectLibraryBorrowList(map);
    }
    /**
     * 通过借阅ID查询借阅
     *
     * @param borrowId 借阅ID
     * @return 借阅对象信息
     */
    @Override
    public LibraryBorrow selectLibraryBorrowById(Long borrowId)
    {
        return libraryBorrowMapper.selectLibraryBorrowById(borrowId);
    }

    /**
     * 批量删除借阅信息
     *
     * @param ids 需要删除的数据ID
     * @throws Exception
     */
    @Override
    @Transactional
    public int deleteLibraryBorrowByIds(String ids)
    {
        Long[] borrowIds = Convert.toLongArray(ids);
        return libraryBorrowMapper.deleteLibraryBorrowByIds(borrowIds);
    }

    /**
     * 新增保存借阅信息
     *
     * @param borrow 借阅信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLibraryBorrow(LibraryBorrow borrow)
    {
        borrow.setBackType(4L);
        // 新增借阅信息
        return libraryBorrowMapper.insertLibraryBorrow(borrow);
    }
    /**
     * 修改借阅还书信息
     *
     * @param borrow 借阅信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLibrarybackBorrow(LibraryBorrow borrow)
    {
        // 修改借阅还书信息
        int i = 0;
        if (borrow.getBackType()<4  && borrow.getBackType()>= 0){
            i = libraryBorrowMapper.updateLibrarybackBorrow(borrow);
        }
        return i;
    }
    /**
     * 校验用户和书籍是否唯一
     *
     * @param bookId  userId 借阅信息
     * @return 结果
     */
    @Override
    public LibraryBorrow checkLibraryBorrowBookidTouserIdUnique(Long bookId,Long userId)
    {
        Long bookid = StringUtils.isNull(bookId) ? -1L : bookId;
        Long userid = StringUtils.isNull(userId) ? -1L : userId;
        HashMap<String, Object> map = new HashMap<>();
        map.put("bookId",bookid);
        map.put("userId",userid);
        LibraryBorrow info = libraryBorrowMapper.checkLibraryBorrowBookidTouserIdUnique(map);
        return  info;
    }

    /**
     * 个人获取图书借阅列表
     *
     * @param borrow 借阅信息
     * @return
     */
    @Override
    public List<LibraryBorrow> selectLibraryBorrowListPerson(LibraryBorrow borrow,String user_name,String name,Integer typeId,Integer userId)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_name",user_name);
        map.put("name",name);
        map.put("typeId",typeId);
        map.put("backType",borrow.getBackType());
        map.put("userId",userId);
        return libraryBorrowMapper.selectLibraryBorrowList(map);
    }

    /**
     * 今日出借阅数量
     * @return 出借阅数量
     */
    @Override
    public int selecttodaylendDate() {
        return libraryBorrowMapper.selecttodaylendDate();
    }
    /**
     * 今日归还数量
     * @return 归还数量
     */
    @Override
    public int selecttodaybackDate() {
        return libraryBorrowMapper.selecttodaybackDate();
    }
}